package com.bluefox.modules.quartz.rest;

import com.bluefox.aop.log.Log;
import com.bluefox.exception.BadRequestException;
import com.bluefox.modules.quartz.domain.QuartzJob;
import com.bluefox.modules.quartz.service.QuartzJobService;
import com.bluefox.modules.quartz.service.dto.JobQueryCriteria;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName QuartzJobController
 * @Description QuartzJobController
 * @Author SunFeng
 * @Date 2020/2/28 15:31
 * @Version 1.0
 */
@Slf4j
@RestController

@RequestMapping("/jobs")
public class QuartzJobController {
    private static final String ENTITY_NAME = "quartzJob";

    private final QuartzJobService quartzJobService;

    public QuartzJobController(QuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }

    /**
     * 获取定时任务列表
     * @param criteria
     * @param pageable
     * @return
     */
    @GetMapping
    @Log("获取定时任务列表")
    @PreAuthorize("@el.check('monitor:job:query1')")
    public ResponseEntity<Object> getJobs(JobQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(quartzJobService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    /**
     * 新增定时任务
     * @param resources
     * @return
     */
    @PostMapping
    // @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody QuartzJob resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        return new ResponseEntity<>(quartzJobService.create(resources),HttpStatus.CREATED);
    }

    /**
     * 根据id获取任务信息
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    //@PreAuthorize("@el.check('timing:edit')")
    public ResponseEntity<Object> findById(@PathVariable Long id){
        return new ResponseEntity<>(quartzJobService.findById(id),HttpStatus.OK);
    }

    /**
     * 修改定时任务
     * @param resources
     * @return
     */
    @PutMapping
    //@PreAuthorize("@el.check('timing:edit')")
    public ResponseEntity<Object> update(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources){
        quartzJobService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 更改定时任务状态
     * @param id
     * @return
     */
    @PutMapping(value = "/updateIsPause/{id}")
    public ResponseEntity<Object> updateIsPause(@PathVariable Long id){
        quartzJobService.updateIsPause(quartzJobService.findById(id));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 执行定时任务
     * @param id
     * @return
     */
    @PutMapping(value = "/exec/{id}")
    public ResponseEntity<Object> execution(@PathVariable Long id){
        quartzJobService.execution(quartzJobService.findById(id));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除定时任务
     * @param jobIds
     * @return
     */
    @DeleteMapping(value = "/{jobIds}")
    public ResponseEntity<Object> delete(@PathVariable Long[] jobIds){
        quartzJobService.delete(jobIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
